package com.achun.getcouponleague.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.achun.getcouponleague.R;
import com.achun.getcouponleague.utils.Logutils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    private Unbinder bind;
    private FrameLayout base_fragment;
    private View successView;
    private Status CurrentStatus=Status.NONE;
    private View loadingView;
    private View errorView;
    private View emptyView;


    public enum Status {
        NONE,LOADING,SUCCESS,EMPTY,ERROR

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View rootView= createRootView(inflater,container);
       base_fragment = rootView.findViewById(R.id.base_fragment);
        loadStatesView(inflater,container,savedInstanceState);
        ButterKnife.bind(this,rootView);
        initView(rootView);
        initPresenter();
        initListener();
     loadData();//加载数据
     return rootView;
    }

    protected void initListener() {
       
    }

    protected View createRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_null,container,false);
    }

    private void loadStatesView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Logutils.e(this.getClass(),"页面加载状态啦");
        //成功的View


            successView = loadSuccessView(inflater,container,savedInstanceState);
            base_fragment.addView(successView);

        //Loading的View


            loadingView = loadLoadingView(inflater,container);
            base_fragment.addView(loadingView);

        //错误页面


            errorView = loadErrorView(inflater,container);
            errorView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //网络错误抛出点击事件
                   onReTry();
                }
            });

            base_fragment.addView(errorView);

        //空页面

            emptyView = loadEmptyView(inflater,container);
            base_fragment.addView(emptyView);

        setViewStatus(Status.NONE);
    }

    protected abstract void onReTry();

    public void setViewStatus(Status status) {
        this.CurrentStatus=status;
        successView.setVisibility(status==Status.SUCCESS?View.VISIBLE:View.GONE);
        loadingView.setVisibility(status==Status.LOADING?View.VISIBLE:View.GONE);
        errorView.setVisibility(status==Status.ERROR?View.VISIBLE:View.GONE);
        emptyView.setVisibility(status==Status.EMPTY?View.VISIBLE:View.GONE);
    }

    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty,container,false);
    }

    protected View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error,container,false);
    }

    protected View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading,container,false);
    }

    protected void initView(View rootview) {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        release();
    }

    protected void release() {
    }

    protected  void initPresenter(){

    }

    protected  void loadData(){

    };

    public abstract View loadSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) ;
    //网络重试点击事件
    public interface onErrorRetrylistener{
        void onclick();
    }
    
}
